//
//  SISSEntollPassengerDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/20.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSEnrollPassengerDetailViewController.h"
#import "SISSMorePopupView.h"
#import "SISSPassengerResourceEnrollViewController.h"
#import "SISSNetworkingManager+Mine.h"

@interface SISSEnrollPassengerDetailViewController ()<SISSMorePopupViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameAndPurposeLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndCommunityLabel;
@property (weak, nonatomic) IBOutlet UITextField *budgetTextField;
@property (weak, nonatomic) IBOutlet UITextField *houseTypeTextField;
@property (weak, nonatomic) IBOutlet UITextField *areaTextField;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *specialDemandTagLabels;
@property (weak, nonatomic) IBOutlet UITextField *intentionFloorTextField;
@property (weak, nonatomic) IBOutlet UITextField *decorationTextField;
@property (weak, nonatomic) IBOutlet UITextField *directionTextField;
@property (weak, nonatomic) IBOutlet UITextField *purchasePurposeTextField;
@property (weak, nonatomic) IBOutlet UILabel *otherDescriptionLabel;
@property (weak, nonatomic) IBOutlet UITextField *buyerNameTextField;
@property (weak, nonatomic) IBOutlet UILabel *buyerIDCardNoLabel;
@property (weak, nonatomic) IBOutlet UITextField *buyerPhoneNumTextField;
@property (weak, nonatomic) IBOutlet UILabel *insertTimeLabel;

@end

@implementation SISSEnrollPassengerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (UILabel *label in self.specialDemandTagLabels) {
        
        label.hidden = YES;
    }
    
    // 添加左侧的返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    // 添加右侧分享删除按钮
    self.navigationItem.rightBarButtonItem = self.isChange ? [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)] : nil;
}

- (void)back {
    
    if (self.deleteComplete) {
        self.deleteComplete();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setPassengerModel:(SISSPassengerResourceModel *)passengerModel {
    [self.view layoutIfNeeded];
    _passengerModel = passengerModel;
    
    NSString *buyerName = passengerModel.buyer_name ? passengerModel.buyer_name : passengerModel.name;
    self.nameAndPurposeLabel.text = [NSString stringWithFormat:@"%@求购",buyerName];
    self.regionAndCommunityLabel.text = [NSString stringWithFormat:@"%@-%@ %@",passengerModel.region,passengerModel.regionDetail,passengerModel.neighbourhood.length > 0 ? passengerModel.neighbourhood : @""];
    self.budgetTextField.text = [NSString stringWithFormat:@"%.0f-%.0f万",passengerModel.minPrice,passengerModel.maxPrice];
    self.houseTypeTextField.text = passengerModel.houseType;
    self.areaTextField.text = [NSString stringWithFormat:@"%.0f-%.0f㎡",passengerModel.minArea,passengerModel.maxArea];
    if (passengerModel.tag && passengerModel.tag.length > 0) {
        NSArray<UIColor *> *labelCololors = @[COLOR(0x23C189), COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00), COLOR(0xE46BD4), COLOR(0xE46C25)];
        NSArray *tags = [passengerModel.tag componentsSeparatedByString:@"#"];
        for (int i = 0; i < tags.count; i++) {
            UILabel *label = self.specialDemandTagLabels[i];
            label.hidden = NO;
            label.layer.borderWidth = 1;
            label.text = [NSString stringWithFormat:@" %@ ",tags[i]];
    //        NSInteger colorIndex = passengerModel.listTag[i].color - 1;
            label.layer.borderColor = labelCololors[i].CGColor;
            label.textColor = labelCololors[i];
            label.layer.cornerRadius = 2;
            label.layer.masksToBounds = YES;
        }
    }
    
    self.intentionFloorTextField.text = (passengerModel.minFloor != 0 && passengerModel.maxFloor != 0) ? [NSString stringWithFormat:@"%zd-%zd层",passengerModel.minFloor,passengerModel.maxFloor]: @"暂无数据";
    self.decorationTextField.text = passengerModel.decoration.length ? passengerModel.decoration : @"暂无数据";
    self.directionTextField.text = passengerModel.direction.length ? passengerModel.direction : @"暂无数据";
    self.purchasePurposeTextField.text = passengerModel.purpose.length ? passengerModel.purpose : @"暂无数据";
    self.otherDescriptionLabel.text = passengerModel.otherdesc.length ? passengerModel.otherdesc : @"暂无数据";
    self.buyerNameTextField.text = passengerModel.name;
    self.buyerIDCardNoLabel.text = passengerModel.idNumber;
    self.buyerPhoneNumTextField.text = passengerModel.phoneNumber;
    self.insertTimeLabel.text = [passengerModel.insertTime substringToIndex:10];
}

/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    if (self.passengerModel.traded == 1 || self.passengerModel.trading == 1) { // 已交易、交易中的只有分享
        self.isChange = NO;
    }
    NSArray *arr = self.isChange ? @[@"修改", @"删除", @"分享"] : @[@"分享"];
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:arr];
    moreView.delegate = self;
    [moreView show];
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    self.abdId = self.passengerModel.ID;
//}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (!self.isChange) {
        
        [self share];
        return;
    }
    
    switch (index) {
        case 0: {
            SISSPassengerResourceEnrollViewController *vc = [[SISSPassengerResourceEnrollViewController alloc] init];
            vc.passengerModel = self.passengerModel;
            vc.title = @"客源修改";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 1: {
            [self deleteHouse];
        }
            break;
            
        case 2: {
            [self share];
        }
            break;
            
        default:
            break;
    }
    
}

- (void)setAbdId:(NSString *)abdId {
    _abdId = abdId;
    
    [kNetworkManager requestPassengerResourceDetailWithPassengerId:abdId successBlock:^(SISSPassengerResourceModel *passengerModel) {
        
        self.passengerModel = passengerModel;
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

- (void)share {
    
    // 根据身份证判断显示 先生、女士
    NSString *fisrtName = [self.passengerModel.name substringToIndex:1];
    NSInteger idNum = [[self.passengerModel.idNumber substringWithRange:NSMakeRange(self.passengerModel.idNumber.length - 2, 1)] integerValue];
    
    NSString *genderStr = (idNum % 2) == 0 ? @"女士" : @"先生";
    NSString *title = [NSString stringWithFormat:@"%@%@ 求购",fisrtName,genderStr];
    NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@-%@ | %@",@(self.passengerModel.minPrice),@(self.passengerModel.maxPrice),self.passengerModel.region,self.passengerModel.regionDetail,self.passengerModel.houseType];
    NSString *webpageUrl = @"";
    if (self.passengerModel.demandType == 1) { // 线上
        webpageUrl = [NSString stringWithFormat:@"%@/?#!/buyerDetail/%@/",WEB_BASE_URL,self.passengerModel.ID];
    } else { // 线下
        webpageUrl = [NSString stringWithFormat:@"%@/?#!/offlineDemandDetail/%@",WEB_BASE_URL,self.passengerModel.ID];
    }
    
    [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:nil];
}


/**
 删除房源
 */
- (void)deleteHouse {
    
    [kNetworkManager deletePassengerResourceWithPassengerId:self.passengerModel.ID successBlock:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 删除成功
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"删除成功！" andTime:1.0 andColor:YES];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.deleteComplete) {
                    self.deleteComplete();
                }
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}



@end
